<html>
<head><meta charset="utf-8"><title>inclusive range patterns · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inclusive.20range.20patterns.html">inclusive range patterns</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229964062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inclusive%20range%20patterns/near/229964062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inclusive.20range.20patterns.html#229964062">(Mar 12 2021 at 02:18)</a>:</h4>
<p>are inclusive range patterns intended to be stable? I ask because exclusive range patterns are unstable, and inclusive range patterns, while accepted on the stable compiler, don't actually seem to work:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="o">..=</span><span class="mi">4</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">][</span><span class="mi">0</span><span class="o">..=</span><span class="mi">4</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"test"</span><span class="p">)</span><span class="w"> </span><span class="c1">// does not print</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="229964159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inclusive%20range%20patterns/near/229964159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inclusive.20range.20patterns.html#229964159">(Mar 12 2021 at 02:20)</a>:</h4>
<p>replacing <code>0..=4</code> with <code>..</code> works as expected, and replacing it with <code>0..5</code> gives a compiler error about the stability of exclusive range patterns</p>



<a name="229964392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inclusive%20range%20patterns/near/229964392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inclusive.20range.20patterns.html#229964392">(Mar 12 2021 at 02:22)</a>:</h4>
<p>using <code>0..5</code> and enabling the feature flag causes the code to compile but not reach the print statement, which suggests to me that this is an accidental exposure of an unimplemented feature</p>



<a name="229966792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inclusive%20range%20patterns/near/229966792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inclusive.20range.20patterns.html#229966792">(Mar 12 2021 at 02:54)</a>:</h4>
<p>The pattern <code>[0..=4]</code> matches a single-element slice with a value of 0 to 4.  A pattern of <code>[..]</code> matches a slice of any length.  The two are quite different.</p>



<a name="229999069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inclusive%20range%20patterns/near/229999069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inclusive.20range.20patterns.html#229999069">(Mar 12 2021 at 09:29)</a>:</h4>
<p>ah, curious. I suppose that suggests that something like <code>if let [a @ ..2, b @ 2..]</code> will never be supported as a way of splitting a slice?</p>



<a name="230047786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/inclusive%20range%20patterns/near/230047786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/inclusive.20range.20patterns.html#230047786">(Mar 12 2021 at 15:30)</a>:</h4>
<p>Yea, I don't think patterns can be used to implement <code>split_at</code>.  I don't recall any discussion about that.</p>
<p>You can do something like <code>[first, second, rest @ ..]</code>, if you want just the first two elements instead of a slice, but that isn't quite the same.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>